﻿using Microsoft.Xrm.Client;
using Microsoft.Xrm.Client.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Text;
using System.Threading.Tasks;
using XIES.Common.CRMOperations;

namespace XIES.PowerShell.Cmdlets
{
    [Cmdlet("Export", "CRMSolution")]
    public class ExportCRMSolutionCmdlet : Cmdlet
    {
        #region Export Solution Options

        [Parameter(Mandatory = true)]
        public bool Managed
        {
            get;
            set;
        }

        [Parameter(Mandatory = false)]
        public bool ExportAutoNumberingSettings
        {
            get;
            set;
        }
        [Parameter(Mandatory = false)]
        public bool ExportCalendarSettings
        {
            get;
            set;
        }
        [Parameter(Mandatory = false)]
        public bool ExportCustomizationSettings
        {
            get;
            set;
        }
        [Parameter(Mandatory = false)]
        public bool ExportEmailTrackingSettings
        {
            get;
            set;
        }
        [Parameter (Mandatory=false)]
        public bool ExportGeneralSettings
        {
            get;
            set;
        }
        [Parameter (Mandatory=false)]
        public bool ExportIsvConfig
        {
            get;
            set;
        }
        [Parameter (Mandatory=false)]
        public bool ExportMarketingSettings
        {
            get;
            set;
        }
        [Parameter (Mandatory=false)]
        public bool ExportOutlookSynchronizationSettings
        {
            get;
            set;
        }
        [Parameter (Mandatory=false)]
        public bool ExportRelationshipRoles
        {
            get;
            set;
        }

        [Parameter(Mandatory = true)]
        public string OutputFolder
        {
            get;
            set;
        }

        [Parameter(Mandatory = true, HelpMessage="Unique name of the solution")]
        public string SolutionName
        {
            get;
            set;
        }

        #endregion

        #region Connection Parameters

        [Parameter(Mandatory = true)]
        public string ConnectionString
        {
            get;
            set;
        }

        #endregion

        #region Cmdlet Operations

        protected override void ProcessRecord()
        {
            WriteVerbose("Exporting CRM Solution " + this.SolutionName);
            CrmConnection connection = CrmConnection.Parse(this.ConnectionString);
            using (OrganizationService organizationService = new OrganizationService(connection))
            {
                ExportCRMSolutionOperation exportCRMSolutionOperation = new ExportCRMSolutionOperation();
                exportCRMSolutionOperation.Execute();
                base.WriteObject(exportCRMSolutionOperation.SolutionFile);
            }
            WriteVerbose("End of exporting CRM Solution " + this.SolutionName);
        }

        #endregion
    }
}
